package com.jt.controller;

import com.jt.pojo.Menu;
import com.jt.pojo.Result;
import com.jt.pojo.Role;
import com.jt.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/allRoleList")
    public Result getRoleList() {
        List<Role> roleList = roleService.getRoleList();
        return  new Result().setCode(200).setData(roleList).setMasege("查询角色列表");
    }

    /**
     * 添加角色信息
     */
    @RequestMapping("/insertRole")
    public Result insertRole(@RequestBody Role role) {
        List<String> listName = roleService.getRoleList().stream().map(Role::getName).collect(Collectors.toList());
       if (listName.contains(role.getName())){
           return  new Result().setCode(500).setData(null).setMasege("角色名重复");
       }
        boolean yesAndNo = roleService.insertRole(role);
        if (!yesAndNo){
            return  new Result().setCode(500).setData(null).setMasege("添加角色失败");
        }
        return  new Result().setCode(200).setData(null).setMasege("添加角色成功");
    }


    /**
     * 查看当前角色拥有的菜单
     */
    @RequestMapping("/selectMenuListByRoleId")
    public Result selectMenuListByRoleId(String roleId) {
         List<Menu> list=roleService.selectMenuListByRoleId(roleId);
         return  new Result().setCode(200).setData(list).setMasege("查看角色菜单成功");
    }
}
